<template>
  <div class="detail-page">
    <van-nav-bar
      left-text="返回"
      @click-left="$router.back(`/article`)"
      fixed
      title="面经详情"
    />
    <header class="header">
      <h1 v-html="detail.content"></h1>
      <p>
        {{ detail.createdAt }} | {{ detail.likeCount }} 浏览量 | {{ detail.views }} 点赞数
      </p>
      <p>
        <img :scr="detail.avatar" alt="" />
        <span>{{ detail.creator }}</span>
      </p>
    </header>
    <main class="body">
      {{ detail.stem }}
      <!-- <p>我是内容</p>
      <p>我是内容</p>
      <p>我是内容</p>
      <p>我是内容</p> -->
    </main>
    <div class="opt">
      <!-- <van-icon  @click="like" :class="{active:arguments.likeFlag}" name="like-o"/>
      <van-icon  @click="star" :class="{active:arguments.collectFlag}" name="star-o"/> -->
      <van-icon @click="like" :class="{active:detail.likeFlag}" name="like-o"/>
      <van-icon @click="star" :class="{active:detail.collectFlag}" name="star-o"/>
    </div>
  </div>
</template>

<script>
import { getArticleDetail, updateCollect, updateLike } from '@/api/article'
// import { getArticle  } from '../api/article'
export default {
  name: 'DetailPage',
  data () {
    return {
      detail: {}
    }
  },
  async created () {
    await this.onLoad()
    console.log(this.$route.params.id)
  },
  methods: {
    async onLoad () {
      const res = await getArticleDetail(this.$route.params.id)
      if (res && res.data) {
        console.log(res.data)
        this.detail = res.data
      }
      // console.log(res)
      // this.detall = res.data
    },
    async like () {
      await updateLike(this.$route.params.id)
      this.detail.likeFlag = !this.detail.likeFlag
      if (this.detail.likeFlag) {
        this.detail.likeCount++
        this.$toast.success('点赞成功')
        console.log(this.detail.likeCount)
      } else {
        this.detail.likeCount--
        this.$toast.success('取消点赞')
        console.log(this.detail.likeCount)
      }
    },
    async star () {
      await updateCollect(this.$route.params.id)
      this.detail.collectFlag = !this.detail.collectFlag
      if (this.detail.collectFlag) {
        // this.detail.collectCount++
        this.$toast.success('收藏成功')
        console.log(this.detail.collectCount)
      } else {
        // this.detail.collectCount--
        this.$toast.success('取消收藏')
        console.log(this.detail.collectCount)
      }
    }

  }
}
</script>

<style lang="less" scoped>
.detail-page {
  margin-top: 44px;
  overflow: hidden;
  padding: 0 15px;
  .header {
    h1 {
      font-size: 24px;
    }
    p {
      color: #999;
      font-size: 12px;
      display: flex;
      align-items: center;
    }
    img {
      width: 40px;
      height: 40px;
      border-radius: 50%;
      overflow: hidden;
    }
  }
  .opt {
    position: fixed;
    bottom: 100px;
    right: 0;
    > .van-icon {
      margin-right: 20px;
      background: #fff;
      width: 40px;
      height: 40px;
      line-height: 40px;
      text-align: center;
      border-radius: 50%;
      box-shadow: 2px 2px 10px #ccc;
      font-size: 18px;
      &.active {
        background: #FEC635;
        color: #fff;
      }
    }
  }
}
</style>
